package interfaces;
import gestion.Bicicletas;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import dominio.Bicicleta;
import excepciones.DAOExcepcion;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class VentanaBicicleta extends javax.swing.JDialog {
	private JLabel jLabelId;
	private JLabel jLabelUltimaEsnCard;
	private JTextField jTextFieldNumeroBastidor;
	private JTextField jTextFieldUltimaEsnCard;
	private JButton jButtonCancelar;
	private JButton jButtonGuardar;
	private JTextArea jTextAreaEstado;
	private JTextArea jTextAreaDescription;
	private JTextField jTextFieldId;
	private JLabel jLabelEstado;
	private JLabel jLabelNumeroBastidor;
	private JLabel jLabelDescription;
	
	private Bicicleta bicicleta = new Bicicleta();
	private Bicicletas gestionBicicleta = new Bicicletas();

	/**
	* Auto-generated main method to display this JDialog
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				VentanaBicicleta inst = new VentanaBicicleta(frame);
				inst.setVisible(true);
			}
		});
	}
	
	public VentanaBicicleta(JFrame frame) {
		super(frame);
		initGUI();
	}
	
	public VentanaBicicleta() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			{
				GridBagLayout thisLayout = new GridBagLayout();
				thisLayout.columnWidths = new int[] {130, 10, 240};
				thisLayout.columnWeights = new double[] {0.0, 0.0, 0.0};
				thisLayout.rowHeights = new int[] {0, 0, 40, 0, 40, 0, 40};
				thisLayout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
				getContentPane().setLayout(thisLayout);
				this.setTitle("ESN Office - Bicicleta");
				{
					jLabelId = new JLabel();
					BorderLayout jLabelTitleLayout = new BorderLayout();
					getContentPane().add(jLabelId, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelId.setText("ID Bicicleta *");
					jLabelId.setLayout(null);
				}
				{
					jLabelDescription = new JLabel();
					getContentPane().add(jLabelDescription, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelDescription.setText("Description");
					jLabelDescription.setLayout(null);
				}
				{
					jLabelNumeroBastidor = new JLabel();
					getContentPane().add(jLabelNumeroBastidor, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelNumeroBastidor.setText("Numero Bastidor *");
					jLabelNumeroBastidor.setLayout(null);
				}
				{
					jLabelEstado = new JLabel();
					getContentPane().add(jLabelEstado, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelEstado.setText("Estado *");
					jLabelEstado.setLayout(null);
				}
				{
					jLabelUltimaEsnCard = new JLabel();
					getContentPane().add(jLabelUltimaEsnCard, new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					jLabelUltimaEsnCard.setText("Ultima ESN Card");
					jLabelUltimaEsnCard.setLayout(null);
				}
				{
					jTextFieldId = new JTextField();
					getContentPane().add(jTextFieldId, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jTextAreaDescription = new JTextArea();
					getContentPane().add(jTextAreaDescription, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jTextFieldNumeroBastidor = new JTextField();
					getContentPane().add(jTextFieldNumeroBastidor, new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jTextFieldUltimaEsnCard = new JTextField();
					getContentPane().add(jTextFieldUltimaEsnCard, new GridBagConstraints(2, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jTextAreaEstado = new JTextArea();
					getContentPane().add(jTextAreaEstado, new GridBagConstraints(2, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				}
				{
					jButtonGuardar = new JButton();
					getContentPane().add(jButtonGuardar, new GridBagConstraints(2, 6, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					jButtonGuardar.setText("Guardar");
					jButtonGuardar.setPreferredSize(new java.awt.Dimension(90, 23));
					jButtonGuardar.setSize(70, 23);
					jButtonGuardar.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
								try {
									jButtonGuardarActionPerformed(evt);
								} catch (HeadlessException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (DAOExcepcion e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
						}
					});
				}
				{
					jButtonCancelar = new JButton();
					getContentPane().add(jButtonCancelar, new GridBagConstraints(2, 6, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					jButtonCancelar.setText("Cancelar");
					jButtonCancelar.setPreferredSize(new java.awt.Dimension(90, 23));
					jButtonCancelar.setSize(70, 23);
					jButtonCancelar.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							jButtonCancelarActionPerformed(evt);
						}
					});
				}
			}
			this.setSize(480, 380);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void jButtonCancelarActionPerformed(ActionEvent evt) {
		System.out.println("jButtonCancelar.actionPerformed, event="+evt);
		//TODO add your code for jButtonCancelar.actionPerformed
		this.dispose();
	}
	
	private void jButtonGuardarActionPerformed(ActionEvent evt) throws HeadlessException, DAOExcepcion {
		System.out.println("jButtonGuardar.actionPerformed, event="+evt);
		//TODO add your code for jButtonGuardar.actionPerformed
		int id = -1;
		if (jTextFieldId.getText().length() != 0) {
			try {
				id = Integer.parseInt(jTextFieldId.getText());
			}
			catch(NumberFormatException nFE) {
				JOptionPane error = new JOptionPane();
				error.showMessageDialog(null, "ID deberia ser un numero", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		
		bicicleta.setIdbicicleta(id); 
		bicicleta.setDescripcion(jTextAreaDescription.getText());
		bicicleta.setNumerobastidor(jTextFieldNumeroBastidor.getText());
		bicicleta.setEstado(jTextAreaEstado.getText());
		bicicleta.setUltimaesncard(jTextFieldUltimaEsnCard.getText());
		
		if(gestionBicicleta.comprobarBicicletaNuevaEsCorrecta(bicicleta)) {
			if (gestionBicicleta.anadirBicicleta(bicicleta))
				this.dispose();
			else {
				JOptionPane error = new JOptionPane();
				error.showMessageDialog(null, "Fallo en guardado de datos", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
}
